package com.movies.dao;

import com.movies.pojo.Cinema;
import com.movies.pojo.Movie;
import com.movies.pojo.Program;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MovieDaoImpl extends BaseDao implements MovieDao {

    //查询全部电影院列表的方法（林燕文）
    public List<Cinema> selectmovie() {
        List<Cinema> cinemas = new ArrayList<Cinema>();
        try {
            this.getConnect();
            String sql = "SELECT  brandName FROM brand ";
            Object[] params = {};
            ResultSet rs = this.query(sql, params);
            while (rs.next()) {
                String brandName = rs.getString("brandName");
                Cinema cinema = new Cinema();
                cinema.setBrandName(brandName);
                cinemas.add(cinema);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.close();
        }
        return cinemas;
    }
    // 实现根据放映日期查询出电影信息方法:查出近期的电影，返回Movie对象（王小艺）
    public List<Movie> getMovieByDateNear(String date) {
        List<Movie> list = new ArrayList<Movie>();
        // 准备好sql语句和数组
        String sql = "SELECT  moviename, totaltime , typename,score  FROM movie m INNER JOIN movietype t ON m.typeid = t.typeid WHERE DATE < ? ";
        Object[] params = {date};
        // 查询出来的结果返回一个结果集
        ResultSet rs = this.query(sql, params);
        try {
            // 循环将结果集遍历出来
            while (rs.next()) {
                // 创建Movie对象
                Movie movie = new Movie();
                String movieName = rs.getString("moviename");
                double score = rs.getDouble("score");
                Integer totalTime = rs.getInt("totaltime");
                String typeName=rs.getString("typename");
                // 依次存进去movie对象里
                movie.setMovieName(movieName);
                movie.setTotalTime(totalTime);
                movie.setScore(score);
                movie.setTypeName(typeName);
                list.add(movie);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.close();

        }
        // 最后将movie对象返回
        return list;
    }
    // 实现根据放映日期查询出电影信息方法:即将放映的电影，返回Movie对象（王小艺）
    public List<Movie> getMovieByDateFar(String date){
        List<Movie> list = new ArrayList<Movie>();
        // 准备好sql语句和数组
        String sql = "SELECT  moviename, totaltime , typename,score  FROM movie m INNER JOIN movietype t ON m.typeid = t.typeid WHERE DATE > ? ";
        Object[] params = {date};
        // 查询出来的结果返回一个结果集
        ResultSet rs = this.query(sql, params);
        try {
            // 循环将结果集遍历出来
            while (rs.next()) {
                // 创建Movie对象
                Movie movie = new Movie();
                String movieName = rs.getString("moviename");
                double score = rs.getDouble("score");
                Integer totalTime = rs.getInt("totaltime");
                String typeName=rs.getString("typename");
                // 依次存进去movie对象里
                movie.setMovieName(movieName);
                movie.setTotalTime(totalTime);
                movie.setScore(score);
                movie.setTypeName(typeName);
                list.add(movie);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.close();
        }
        // 最后将movie对象返回
        return list;
    }
    /**根据影院id和电影id、日期来查询排挡
     * SELECT m.moviename,m.date,begintime,endtime,l.languagename,h.hallname,price FROM program p JOIN `language` l ON l.`languageid` = p.`languageid` JOIN hall h ON h.`hallid`=p.`hallid` JOIN movie m ON  m.`movieid`=p.`movieid` WHERE cinemaid=400  AND playdate='2019-07-03' AND p.movieid = 5000 ORDER BY begintime ASC;
     * 然后通过进入影院主页携带的影院id和默认日期，在通过电影id来分类展示电影信息
     * 黄键松
     *  */
    @Override
    public List<Program> programShow(int cinemaid, int movieid, String date) {
        String sql = "SELECT m.`score`, m.moviename,m.date,begintime,endtime,l.languagename,h.hallname,price " +
                "FROM program p JOIN `language` l ON l.`languageid` = p.`languageid` JOIN hall h ON h.`hallid`=p.`hallid` " +
                "JOIN movie m ON  m.`movieid`=p.`movieid` " +
                "WHERE cinemaid=?  AND playdate=? AND p.movieid = ? ORDER BY begintime ASC;";
        Object[] obj = {cinemaid, movieid, date};
        ResultSet rs = query(sql,obj);
        Program program = null;
        List list = new ArrayList();
        try{
            while(rs.next()){
                program = new Program();
                program.setScore(rs.getDouble("score"));
                program.setMovieName(rs.getString("moviename"));
                program.setPlayDate(rs.getString("date"));
                program.setBeginTime(rs.getString("begintime"));
                program.setEndTime(rs.getString("endtime"));
                program.setLanguage(rs.getString("language"));
                program.setHallName(rs.getString("hallname"));
                program.setPrice(rs.getDouble("price"));
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally {
            close();
        }
        return list;
    }

    @Override
    /**创建一个查询所有电影院信息方法（黄建松）*/
    public List queryAllCinema() {
        return null;
    }
    @Override
    /**创建一个查询电影院主页信息方法
     *通过页面携带的影院id跳转到该主页
     * 通过影院id查询影院信息
     * （黄建松）
     * */
    public List<Cinema> queryCinema(int cinemaid) {
        String sql = "SELECT brandname, address,phone,`server` " +
                "FROM cinema c JOIN brand b ON c.`brandid`=b.`brandid` WHERE cinemaid = ?;";
        Object[] obj = {cinemaid};
        ResultSet rs = query(sql,obj);
        Cinema cinema = null;
        List list = new ArrayList();
        try{
            while(rs.next()){
                cinema = new Cinema();
                cinema.setBrandName(rs.getString("brandname"));
                cinema.setAddress(rs.getString("address"));
                cinema.setPhone(rs.getString("phone"));
                cinema.setServer(rs.getString("server"));
                list.add(cinema);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally {
            close();
        }
        return list;
    }
    @Override
    /**创建一个查询播放档期计划方法
     * 通过页面提交的电影和影院id
     * 查询对应的档期
     * （黄建松）
     * */
    public List<Program> queryProgram(int cinemaid,int movieid) {
        String sql = "SELECT begintime,endtime,l.languagename,h.hallname,price " +
                "FROM program p JOIN `language` l ON l.`languageid` = p.`languageid` " +
                "JOIN hall h ON h.`hallid`=p.`hallid` WHERE cinemaid=? AND movieid = ? AND  playdate=?";
        Object[] obj = {cinemaid,movieid};
        ResultSet rs = query(sql,obj);
        Program program = null;
        List list = new ArrayList();
        try{
            while (rs.next()){
                program = new Program();
                program.setBeginTime(rs.getString("begintime"));
                program.setEndTime(rs.getString("endtime"));
                program.setLanguage(rs.getString("languagename"));
                program.setHallName(rs.getString("hallname"));
                program.setPrice(rs.getDouble("price"));
                list.add(program);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            close();
        }
        return list;
    }
}
